# -*- coding:utf-8 -*-
from flask import request, jsonify, abort
from flask_cors import CORS
from com.zzh.dao_v2.shareDao import ShareDao
import time
from . import app

localTime = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))


CORS(app, supports_credentials=True)

"""
        表名：zzh_ueser_amount_log

        说明：会员金额日志表
"""

@app.route('/queryShareById', methods=['POST'])
def queryShareById():

    if not request.json:
        abort(400)
    id = request.json.get("id")
    try:
        total_count, userCodeJSON= ShareDao().queryShareById(id)
        if total_count:
            return jsonify({"message": {"code": 1, "status": "queryShare OK"},"info":userCodeJSON})
        else:
            return jsonify({"info": [], "message": {"code": 0, "status": "No data"}})
    except Exception, e:
            return jsonify({"message": {"code": 0,  "info": str(e)}})


@app.route('/queryShareByCondition', methods=['POST'])
def queryShareByCondition():

    if not request.json:
        abort(400)
    userId = request.json.get("userId")
    pageIndex = request.json.get("pageIndex",1)#传递页面索引号，默认为1
    pageSize = request.json.get("pageSize",10)
    try:
        total_count, UserCodeJSON = ShareDao().queryShareByCondition(userId=userId,pageIndex=pageIndex,pageSize=pageSize)
        if UserCodeJSON:
                return jsonify({"message": {"code": 1, "status": "queryShare OK"},"total":total_count,"info":UserCodeJSON})
        else:
            return jsonify({"info": [], "message": {"code": 0, "status": "No data"}})

    except Exception, e:
            return jsonify({"message": {"code": 0,  "info": str(e)}})



@app.route('/insertShare', methods=['POST'])
def insertShare():

    if not request.json:
        abort(400)

    Info = {

        'userId': request.json.get('userId'),  #
        'userName': request.json.get('userName'),  #
        'shareContent': request.json.get('shareContent'),  #
        'sharePrivacy': request.json.get('sharePrivacy'),#
        'shareCommentCount':request.json.get('shareCommentCount'),
        'shareStatus':request.json.get('shareStatus')


    }
    if not Info:
        return jsonify({"message": {"code": 0, "info": "信息填写未完整"}})
    try:
        rs = ShareDao().addShare(Info)

        if rs:
            return jsonify(
                {  "massage": {"code": 1, "info": "addShare OK"}})  # 添加函数调用成功，返回添加的信息（不包括userRecharge的Id）
        else:
            return jsonify({"message": {"code": 0, "info": "addShare Error"}})  # 添加函数调用失败\
    except Exception, e:
            return jsonify({"message": {"code": 0,  "info": str(e)}})



@app.route('/updateShare', methods=['POST'])
def updateShare():#
    try:
        id = request.json['id']
    except:
        return jsonify({"message": {"code": 0, "status": "请传入 id"}})
    try:
        oldInfo = ShareDao().selectShare(id)
    except Exception, e:
            return jsonify({"message": {"code": 0,  "info": str(e)}})

    Info = {
         'id': request.json.get('id'),  #
        'userId': request.json.get('userId',oldInfo[1]),  #
        'userName': request.json.get('userName',oldInfo[2]),  #
        'shareContent': request.json.get('shareContent',oldInfo[3]),  #
        'sharePrivacy': request.json.get('sharePrivacy',oldInfo[5]),#
        'shareCommentCount':request.json.get('shareCommentCount',oldInfo[6]),
        'shareStatus':request.json.get('shareStatus',oldInfo[7])

    }
    if not Info:
        return jsonify({"message": {"code": 0, "info": "信息填写未完整"}})
    try:
        rs = ShareDao().updateShare(Info)
        if rs:
            return jsonify(
                {"info": Info, "massage": {"code": 1, "info": "updateShare OK"}})  # 添加函数调用成功，返回添加的信息
        else:
            return jsonify({"message": {"code": 0, "info": "updateShare Error"}})  # 添加函数调用失败
    except Exception, e:
        return jsonify({"message": {"code": 0, "info": str(e)}})




@app.route('/deleteShare', methods=['POST'])  # 根据id数组删除zzh_user_Code表指定记录,传入格式为 "ids":[1,2,3,4]
def deleteShare():
    ids = request.json.get("ids")
    if not ids:
        return jsonify({"message":"请输入待删除的  id数组！"})
    try:
        rs = ShareDao().deleteShare(ids)
        if rs:
            return jsonify({"message": {"code": 1, "info": "deleteShare OK"}})
        else:
            return jsonify({"message": {"code": 0, "info": "deleteShare Error"}})
    except Exception, e:
            return jsonify({"message": {"code": 0,  "info": str(e)}})


